package ynu.edu.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Entity
@Getter
@Setter
@DiscriminatorValue(User.TYPE_BUSINESS)
public class Business extends User {
    @OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
    @JoinColumn(name = "business_id")
    @JsonIgnoreProperties(value = {"business"})
    private Set<Shop> shops;

    @OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
    @JoinColumn(name = "business_id")
    @JsonIgnoreProperties(value = {"business"})
    private Set<Item> items;

    @JsonIgnore
    public Shop createShop(Shop shop) {
        shop.setBusiness(this);
        getShops().add(shop);
        return shop;
    }

    @JsonIgnore
    public Item createItem(Item item) {
        item.setBusiness(this);
        getItems().add(item);
        return item;
    }

    @JsonIgnore
    public List<Shop> listMyShops() {
        return new ArrayList<>(getShops());
    }

    @JsonIgnore
    public List<Item> listMyItems() {
        return new ArrayList<>(getItems());
    }
}
